[3.9] gh-133767: Fix use-after-free in the unicode-escape decoder with an error handl...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 2 Jun 2025 15:58:01 +0000 (18:58 +0300)
committerAndrej Shadura <andrewsh@debian.org>
Tue, 20 Jan 2026 10:45:10 +0000 (11:45 +0100)
commitb0bacd829690746994283e0d9adf619c4a23c183
tree27e181eb9b2b92214fc5745581e6c1c5e1e1f037
parent2e5e6e28fb7172981e261004defcfe560fc3eb14
[3.9] gh-133767: Fix use-after-free in the unicode-escape decoder with an error handler (GH-129648) (GH-133944) (#134346)

* [3.9] gh-133767: Fix use-after-free in the unicode-escape decoder with an error handler (GH-129648) (GH-133944)

If the error handler is used, a new bytes object is created to set as
the object attribute of UnicodeDecodeError, and that bytes object then
replaces the original data. A pointer to the decoded data will became invalid
after destroying that temporary bytes object. So we need other way to return
the first invalid escape from _PyUnicode_DecodeUnicodeEscapeInternal().

_PyBytes_DecodeEscape() does not have such issue, because it does not
use the error handlers registry, but it should be changed for compatibility
with _PyUnicode_DecodeUnicodeEscapeInternal().
(cherry picked from commit 9f69a58623bd01349a18ba0c7a9cb1dad6a51e8e)
(cherry picked from commit 6279eb8c076d89d3739a6edb393e43c7929b429d)
(cherry picked from commit a75953b347716fff694aa59a7c7c2489fa50d1f5)
(cherry picked from commit 0c33e5baedf18ebcb04bc41dff7cfc614d5ea5fe)
(cherry picked from commit 8b528cacbbde60504f6ac62784d04889d285f18b)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Origin: upstream, https://github.com/python/cpython/commit/8d35fd1b34935221aff23a1ab69a429dd156be77

Gbp-Pq: Name CVE-2025-4516-6.patch
Include/cpython/bytesobject.h
Include/cpython/unicodeobject.h
Lib/test/test_codeccallbacks.py
Lib/test/test_codecs.py
Misc/NEWS.d/next/Security/2025-05-09-20-22-54.gh-issue-133767.kN2i3Q.rst [new file with mode: 0644]
Objects/bytesobject.c
Objects/unicodeobject.c
Parser/pegen/parse_string.c